/*
Copyright (c) 2006 - 2008  Universitaet Osnabrueck, virtUOS
Authors: Markus Ketterl, Johannes Emden, Nils Schuhmacher, Andreas Rickling

This file is part of virtPresenter.

virtPresenter is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

virtPresenter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with virtPresenter.  If not, see <http://www.gnu.org/licenses/>.
*/

package virtPresenter.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import mx.core.Application;
	
	import virtPresenter.event.*;
	import virtPresenter.event.MainWindowPreviewEvent;
	import virtPresenter.model.virtPresenterLocator;
	
	/**
	 * Command lets the presentation show up a preview of the given slide in the main presentation window.
	 * */
  	public class MainWindowPreview implements ICommand {
				
		/**
		 * Handler for BigSlidePreviewEvent. Lets the preview slide show up in the big slide window
		 * 
		 * @see virtPresenter.event.BigSlidePreviewEvent;
		 * 
		 * */				
		
		private static var slideReset:Boolean = false; 
		private static var slideChange:Boolean = false;
		
		public function execute( event : CairngormEvent ): void {
				
			try{
				var model : virtPresenterLocator = virtPresenterLocator.getInstance();
				var thisEvent : MainWindowPreviewEvent = MainWindowPreviewEvent( event );
				var currentSlide:int = model.currentSlideID;
				var currentAnimStep:int = model.currentAnimStep;
				var slide:int;
				if (thisEvent.slideToShow.match(MainWindowPreviewEvent.CURR)){
					if (slideReset){
						model.mainWindowProxy.requestSlide(Number(model.currentSlideID));
						slideReset = false;
					}
					model.mainWindowProxy.requestAnim(Number(model.currentAnimStep));
					if (model.prevAS>=model.currentAnimStep) slideChange = true;
				}
				if (thisEvent.slideToShow.match(MainWindowPreviewEvent.NEXT)){
					if (model.nextAS==0){
						slideReset = true;
						model.mainWindowProxy.requestSlide(Number(model.currentSlideID+1));
					}
					else {
						model.mainWindowProxy.requestAnim(Number(model.nextAS))
					}
				}
				if (thisEvent.slideToShow.match(MainWindowPreviewEvent.PREV)){
					if (model.prevAS>=model.currentAnimStep){
						slideReset = true;
						if (slideChange){
							slideChange = false;
							model.mainWindowProxy.requestSlide(Number(model.currentSlideID-1));
						}
						var animSteps:Array = model.xmlParser.getAnimStepsOfASlide(model.currentSlideID-1);
						model.mainWindowProxy.requestAnim(Number(animSteps.length-1));
					}
					else{
						model.mainWindowProxy.requestAnim(Number(model.prevAS));
					}			
				}
			}
			catch(err:Error){}
			catch(error:ArgumentError) {
				trace ("BigSlidePreview. Error in sendingLC!")
			}
		}		
	}	
}